<template>
  <div>
    <h1>三级分类列表</h1>
    <!-- node-key默认展开 需要该值唯一      :expand-on-click-node为false单击不展开 -->
    <el-tree node-key="catId" :default-expanded-keys="defaultExpandedKey" :expand-on-click-node="false" :data="data" :props="defaultProps">
      <span class="custom-tree-node" slot-scope="{ node, data }">
        <span>{{ node.label }}</span>
        <span>
        <!-- @click="category={sort:0}赋初始值 ;append(data)"清空每次添加遗留的信息 -->
          <el-button
            v-if="data.catLevel != 3"
            type="text"
            size="mini"
            @click="category={sort:0} ;append(data)"
          >
            Append
          </el-button>
          <el-button type="text" size="mini" @click="() => Update(data)">
            Update
          </el-button>
          <!-- 判断node包含的下一级数量为零可以删除 不为零有数据不可以删除 -->
          <el-button
            v-if="node.childNodes.length == 0"
            type="text"
            size="mini"
            @click="() => remove(node, data)"
          >
            Delete
          </el-button>
        </span>
      </span>
    </el-tree>
    <!-- 编辑弹窗 -->
    <el-button type="text" @click="dialogFormVisible = true"
      >打开嵌套表单的 Dialog</el-button
    >

    <el-dialog title="三级分类编辑" :visible.sync="dialogFormVisible">
      <el-form :model="category">
        <el-form-item label="分类名称" label-width="120px">
          <el-input v-model="category.name" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      defaultExpandedKey:[],
      pid: 0,
      category: {
        sort: 0,
      },
      dialogFormVisible: false,
      data: [],
      defaultProps: {
        label: "name",
      },
    };
  },
  components: {},
  created() {
    this.getDataList();
  },
  methods: {
    //删除
    remove(node, data) {
      //声明数组
      let ids = [data.catId];
      //打印
      console.log(node, data);
      this.$http({
        //添加路径            拼接                  删除
        url: this.$http.adornUrl(`/product/category/delete`),
        method: "post",
        //this.category为true表示带参数 为false表示不带参数
        data: this.$http.adornData(ids, false),
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.$message({
            message: "操作成功",
            type: "success",
            duration: 1500,
          });
          //弹窗关闭
          this.dialogFormVisible = false;
          //重新查一次列表
          this.getDataList();
        } else {
          this.$message.error(data.msg);
        }
      });
    },
    //修改 回显
    Update(data) {
      //弹窗
      this.dialogFormVisible = true;
      //回显
      this.$http({
        //查询路径                  拼接
        url: this.$http.adornUrl(`/product/category/info/${data.catId}`),
        method: "get",
        params: this.$http.adornParams(),
      }).then(({ data }) => {
        //打印
        console.log(data);
        //回显
        this.category = data.category;
      });
    },

    //保存
    /**
     * 使用已知条件求未知条件
     *
     * 已知条件
     * data被点击的对象
     * catId
     * level
     * name
     * parentCid
     *
     * 未知条件
     * name         文本框的值
     * parent_id    上一级的id
     * cat_level    上一级层级+1 data.lelvl+1
     * sort:0
     *
     */
    save() {
      //自定义默认添加路径
      let methodUel = "save";
      //判断是否存在id 不存在为添加 存在修改
      if (this.category.catId) {
        //存在修改
        methodUel = "update";
      }
      //添加
      this.$http({
        //添加路径            拼接                 存在id修改不存在添加
        url: this.$http.adornUrl(`/product/category/${methodUel}`),
        method: "post",
        //this.category为true表示带参数 为false表示不带参数
        data: this.$http.adornData(this.category, false),
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.$message({
            message: "操作成功",
            type: "success",
            duration: 1500,
          });
          //弹窗关闭
          this.dialogFormVisible = false;
          //重新查一次列表
          this.getDataList();
          //默认展开                   当前列表的父元素
          this.defaultExpandedKey = [this.category.parentCid]
        } else {
          this.$message.error(data.msg);
        }
      });
    },
    //编辑弹框
    append(data) {
      //打印
      console.log("data", data);
      //弹窗展示
      this.dialogFormVisible = true;
      //接收被点击对象的id赋值
      this.category.parentCid = data.catId;
      //层级赋值
      this.category.catLevel = data.catLevel + 1;
    },

    //三级分类列表查询
    getDataList() {
      this.dataListLoading = true;
      //1. $http是什么
      //2. adornUrl('/sys/menu/list')这个路径怎么和网关拼接
      //3. 参数是怎么传递的
      this.$http({
        //路径
        url: this.$http.adornUrl("/product/category/tree/list"),
        method: "get",
        params: this.$http.adornParams(),
      }).then(({ data }) => {
        console.log(data);
        this.data = data.data;
      });
    },
  },
};
</script>
